import Loki from 'lokijs'

export const db = new Loki('cars', {
    autoload: true,
    autoloadCallback: databseInitialize,
    autosave: true,
    autosaveInterval: 3000,
    persistenceMethod: 'localStorage'
})

function databseInitialize() {
    const comments = db.getCollection('comments')
    if (comments == null) {
        db.addCollection('comments')

    }
}


export function loadCollection(collection) {
    return new Promise(resolve => {
        db.loadDatabase({}, () => {
            const _collection = db.getCollection(collection)
            resolve(_collection)
        })
    })
}